package com.yidu.hbats.service.impl.emp;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.dao.AtsEmpAthleteMapper;
import com.yidu.hbats.dao.AtsEmpCoachMapper;
import com.yidu.hbats.model.AtsEmpAthlete;
import com.yidu.hbats.model.AtsEmpCoach;
import com.yidu.hbats.service.emp.AtsEmpCoachService;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.service.TuserService;
import com.yidu.springbootdemo.permission.tools.Tools;
import com.yidu.utils.Message;
import com.yidu.utils.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @program: hbats
 * @description: 教练员信息接口实现
 * @author: 张超
 * @create: 2019-10-30 10:13
 */
@Service
public class AtsEmpCoachServiceImpl implements AtsEmpCoachService {


    @Resource
    AtsEmpCoachMapper atsEmpCoachMapper;

    @Resource
    TuserService tuserService;

    @Resource
    AtsEmpAthleteMapper atsEmpAthleteMapper;


    @Override
    public Message deleteAll(String ids) {
        Message message = new Message();
        if(StringUtils.isEmpty(ids)){
            message.setNum(0);
            message.setMsg("删除失败,数据错误");
        }else{
            String[] idss = ids.split(",");
            for(int i = 0;i<idss.length;i++){

                //先判断一下教练员下面是不是有运动员
                AtsEmpAthlete atsEmpAthlete = new AtsEmpAthlete();
                atsEmpAthlete.setCoachId(Integer.valueOf(idss[i]));
                int rows = atsEmpAthleteMapper.findAll(atsEmpAthlete).size();
                if(rows>=1){
                    message.setNum(0);
                    message.setMsg("删除失败！第"+(i+1)+"行教练下存在运动员信息，请先删除运动员！");
                    return message;
                }
                AtsEmpCoach atsEmpCoach = new AtsEmpCoach();
                atsEmpCoach.setCoachId(Integer.valueOf(idss[i]));
                atsEmpCoach.setIsva(2);
                saveOrUpdate(atsEmpCoach);
            }
            message.setNum(1);
            message.setMsg("删除成功");
        }
        return message;
    }


    @Override
    public AtsEmpCoach findById(Integer id) {
        return atsEmpCoachMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AtsEmpCoach> selectAll(AtsEmpCoach atsEmpCoach, Page page) {
        PageHelper.offsetPage(page.getStart(), page.getRows());

        List<AtsEmpCoach> list = atsEmpCoachMapper.selectAll(atsEmpCoach);

        PageHelper.startPage(page.getStart(), page.getRows());
        return list;
    }

    @Override
    public int selectCount(AtsEmpCoach atsEmpCoach) {
        return atsEmpCoachMapper.selectCount(atsEmpCoach);
    }

    @Override
    public List<AtsEmpCoach> findAll(AtsEmpCoach atsEmpCoach) {
        return atsEmpCoachMapper.findAll(atsEmpCoach);
    }

    @Override
    public List<AtsEmpCoach> findxiaAll() {
        return atsEmpCoachMapper.findxiaAll();
    }

    @Override
    public Message saveOrUpdate(AtsEmpCoach atsEmpCoach) {
        Message message = new Message();
        if(StringUtils.isEmpty(atsEmpCoach.getCoachId())){
            atsEmpCoachMapper.insertSelective(atsEmpCoach);

            Tuser tuser = new Tuser();
            String rd = UUID.randomUUID().toString().replaceAll("-", "");
            tuser.setUserId(rd);
            tuser.setFilId(atsEmpCoach.getCoachId().toString());//对应对象id
            tuser.setCompanyNum("2");//类型字段 对应 1 , 领导  2,教练员 3,专家 4,运动员
            tuser.setCompanyName(atsEmpCoach.getCoachName());//对象名称
            tuser.setCompany("教练员");//对象类型名称
            tuser.setUserName(atsEmpCoach.getCoachName());
            tuser.setUserPassword("123456");
            tuser.setUserState("Y");
            tuser.setUserFoundDate(Tools.time());
            tuser.setUserFoundUser(atsEmpCoach.getCreater());


            tuserService.add(tuser);
            message.setNum(1);
            message.setMsg("新增成功");

        }else{

            //先判断一下教练员下面是不是有运动员
            if(atsEmpCoach.getIsva()!=null && atsEmpCoach.getIsva()==2){
                AtsEmpAthlete atsEmpAthlete = new AtsEmpAthlete();
                atsEmpAthlete.setCoachId(atsEmpCoach.getCoachId());
                int rows = atsEmpAthleteMapper.findAll(atsEmpAthlete).size();
                if(rows>=1){
                    message.setNum(0);
                    message.setMsg("该教练下存在运动员信息，请先删除运动员！");
                    return message;
                }
            }

            atsEmpCoachMapper.updateByPrimaryKeySelective(atsEmpCoach);

            Tuser tuser = new Tuser();
            tuser.setFilId(atsEmpCoach.getCoachId().toString());
            tuser.setCompanyNum("2");

            Tuser tuser1 = tuserService.findBycompanyNum(tuser);

            if(tuser1!=null){
                //如果是删除的话 就把用户表中的也删除
                if(atsEmpCoach.getIsva()!=null && atsEmpCoach.getIsva()==2){
                    if(tuser1!=null) {
                        tuserService.delete(tuser1.getUserId());
                    }
                }else{
                    tuser1.setCompanyName(atsEmpCoach.getCoachName());//对象名称
                    tuser1.setUserName(atsEmpCoach.getCoachName());
                    tuserService.update(tuser1);
                }
            }

            message.setNum(1);
            message.setMsg("修改成功");
        }

        return message;
    }
}